package cn.dapeng.interview;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;

/**
 * 实现一种猫狗队列结构，要求如下
 * <p>
 * 1. 用户可以调用add方法将cat类或dog类的实例放入队列中
 * 2. 用户可以调用pollAll方法，将队列中所有的实例按照队列的先后顺序依次弹出
 * 3. 用户可以调用pollDog方法，将队列中dog类的实例按照进队列的先后顺序依次弹出
 * 4. 用户可以调用pollCat方法，将队列中dog类的实例按照进队列的先后顺序依次弹出
 * 5. 用户可以调用isEmpty方法，检查队列中是否还有dog或cat实例
 * 6. 用户可以调用isDogEmpty方法，检查队列中是否还有Dog实例
 * 7. 用户可以调用isCatEmpty方法，检查队列中是否还有Cat实例
 */
public class Cat_Dog_Queue_T01 {


    /************************************************************************/
    static class Pet {
        private String type;

        public Pet(String type) {
            this.type = type;
        }

        public String getPetType() {
            return type;
        }

    }

    static class Cat extends Pet {
        public Cat() {
            super("cat");
        }
    }

    static class Dog extends Pet {
        public Dog() {
            super("dog");
        }
    }

    /************************************************************************/

    static class PetEnter {
        private final Pet pet;
        private final long count;

        public PetEnter(Pet pet, long count) {
            this.pet = pet;
            this.count = count;
        }

        public Pet getPet() {
            return pet;
        }

        public long getCount() {
            return count;
        }
    }

    static class CatDogQueue {
        private Queue<PetEnter> catQueue = new LinkedList<>();
        private Queue<PetEnter> dogQueue = new LinkedList<>();
        private long count = 0;

        public void add(Pet pet) {
            if (pet instanceof Cat) {
                catQueue.add(new PetEnter(pet, count++));
            } else if (pet instanceof Dog) {
                dogQueue.add(new PetEnter(pet, count++));
            }
        }

        public PetEnter pooAll() {
            if (!catQueue.isEmpty() && !dogQueue.isEmpty()) {
                if (catQueue.peek().count < dogQueue.peek().count) {
                    return catQueue.poll();
                } else {
                    return dogQueue.poll();
                }
            } else if (!catQueue.isEmpty()) {
                return catQueue.poll();
            } else if (!dogQueue.isEmpty()) {
                return dogQueue.poll();

            }
            return null;
        }

        public PetEnter pollCat() {
            if (!catQueue.isEmpty()) {
                return catQueue.poll();
            }
            return null;
        }

        public PetEnter pollDog() {
            if (!dogQueue.isEmpty()) {
                return dogQueue.poll();
            }
            return null;
        }

        public boolean isEmpty() {
            return catQueue.isEmpty() && dogQueue.isEmpty();
        }

        public boolean isCatEmpty() {
            return catQueue.isEmpty();
        }

        public boolean isDogEmpty() {
            return dogQueue.isEmpty();
        }
    }

    public static void main(String[] args) {
        CatDogQueue queue = new CatDogQueue();
        Random r = new Random();
        for (int i = 0; i < 10; i++) {
            int n = r.nextInt(100);

            if (n % 2 == 0) {
                queue.add(new Cat());
            } else {
                queue.add(new Dog());
            }
        }

//        while (!queue.isEmpty()) {
//            PetEnter p = queue.pooAll();
//            System.out.println(p.getCount() + " == " + p.getPet().getPetType());
//        }

        while (!queue.isCatEmpty()) {
            PetEnter p = queue.pollCat();
            System.out.println(p.getCount() + " == " + p.getPet().getPetType());
        }

        while (!queue.isDogEmpty()) {
            PetEnter p = queue.pooAll();
            System.out.println(p.getCount() + " == " + p.getPet().getPetType());
        }
    }
}
